<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文本转语音测试</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
        }
        textarea, input[type="text"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        textarea {
            min-height: 100px;
            resize: vertical;
        }
        button {
            background-color: #1677ff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #0e5ecd;
        }
        .result {
            margin-top: 30px;
            border-top: 1px solid #eee;
            padding-top: 20px;
        }
        .error {
            color: red;
            margin-top: 10px;
        }
        .loading {
            text-align: center;
            margin-top: 20px;
            display: none;
        }
        .audio-player {
            width: 100%;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>文本转语音测试</h1>
        
        <div class="form-group">
            <label for="text">输入要转换的文本：</label>
            <textarea id="text" placeholder="请输入要转换为语音的文本内容...">您好，这是一段测试文本，用于测试文本转语音功能。</textarea>
        </div>
        
        <div class="form-group">
            <label for="user">用户ID：</label>
            <input type="text" id="user" value="test_user">
        </div>
        
        <button id="convert">转换为语音</button>
        
        <div class="loading" id="loading">
            <p>正在转换中，请稍候...</p>
        </div>
        
        <div class="result" id="result" style="display:none;">
            <h3>转换结果：</h3>
            <audio id="audio-player" class="audio-player" controls></audio>
            <div class="error" id="error"></div>
        </div>
    </div>
    
    <script>
        document.getElementById('convert').addEventListener('click', async function() {
            // 显示加载状态
            document.getElementById('loading').style.display = 'block';
            document.getElementById('result').style.display = 'none';
            document.getElementById('error').textContent = '';
            
            const text = document.getElementById('text').value;
            const user = document.getElementById('user').value;
            
            if (!text) {
                document.getElementById('error').textContent = '请输入要转换的文本';
                document.getElementById('loading').style.display = 'none';
                document.getElementById('result').style.display = 'block';
                return;
            }
            
            try {
                // 创建表单数据
                const formData = new FormData();
                formData.append('text', text);
                formData.append('user', user);
                
                // 创建时间戳防止缓存
                const timestamp = new Date().getTime();
                
                // 创建音频元素的URL
                const audioPlayer = document.getElementById('audio-player');
                audioPlayer.src = `/text-to-audio?text=${encodeURIComponent(text)}&user=${encodeURIComponent(user)}&t=${timestamp}`;
                
                // 添加加载完成事件
                audioPlayer.onloadeddata = function() {
                    document.getElementById('loading').style.display = 'none';
                    document.getElementById('result').style.display = 'block';
                };
                
                // 添加错误处理
                audioPlayer.onerror = function(e) {
                    console.error('音频加载错误:', e);
                    
                    // 尝试获取错误详情
                    fetch(`/text-to-audio?text=${encodeURIComponent(text)}&user=${encodeURIComponent(user)}`)
                        .then(response => {
                            if (!response.ok) {
                                return response.json().then(data => {
                                    throw new Error(data.detail || '未知错误');
                                });
                            }
                            return response;
                        })
                        .catch(err => {
                            document.getElementById('error').textContent = '加载音频失败: ' + err.message;
                        })
                        .finally(() => {
                            document.getElementById('loading').style.display = 'none';
                            document.getElementById('result').style.display = 'block';
                        });
                };
                
                // 加载音频
                audioPlayer.load();
                
            } catch (error) {
                document.getElementById('error').textContent = '转换失败: ' + error.message;
                document.getElementById('loading').style.display = 'none';
                document.getElementById('result').style.display = 'block';
            }
        });
    </script>
</body>
</html> 